﻿using System;
using System.Runtime.InteropServices;
using TecIntern.Interface;
using TecLogger;
using Microsoft.Office.Interop.Excel;
using TecBaseObjects.Base;
using TecTools;
using TecExtensions;
using TecBaseObjects.Objects;
using TecData.Manager;
using TecContracts;

namespace TecIntern {

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("B804DF70-F71E-4381-9C3C-6447D0B1E3AF")]

    [ProgId("TpIntern.Work")]
    public class Work : AbstractExcelTools, IExcelInterface {

        #region Members

        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "TpIntern.Work";
        
        private struct FC {

            #region Menüeinträge
            //Ansicht
            public static int ChangeView = 1;
            public static int ChangeViewWeek = 2;
            public static int ChangeViewArea = 3;
            public static int ChangeViewUser = 4;
            public static int ChangeViewTorprofi = 5;
            public static int ChangeViewVr = 6;
            public static int ChangeViewD2 = 7;
            public static int ChangeViewLeistung = 8;
            public static int ChangeViewOrganisation = 9;

            //Export Intern
            public static int ExportInterneLeistungen = 17;
            public static int ExportLeistungenIntern = 18;

            #endregion

            #region Sonstige Events

            //21 ...
            public static int CheckWorkSheets = 21;
            public static int SetRightMouseMenu = 22;

            public static int WBActivate = 25;
            public static int WBAfterSave = 26;
            public static int WBBeforeClose = 27;
            public static int WBBeforeSave = 28;
            public static int WBDeactivate = 29;
            public static int WBOpen = 30;
            public static int WBSheetActivate = 31;
            public static int WBSheetCalculate = 32;
            public static int WBSheetChange = 33;
            public static int WBSheetSelectionChange = 34;
            public static int WBBeforePrint = 35;
            public static int WBSheetDeactivate = 36;
            public static int WBAddinInstall = 37;
            public static int WBAddinUninstall = 38;
            public static int WBAfterXmlExport = 39;
            public static int WBAfterXmlImport = 40;
            public static int WBBeforeXmlExport = 41;
            public static int WBBeforeXmlImport = 42;
            public static int WBNewChart = 43;
            public static int WBNewSheet = 44;
            public static int WBSheetBeforeDoubleClick = 45;
            public static int WBSheetBeforeRightClick = 46;
            public static int WBSheetFollowHyperlink = 47;
            public static int WBWindowActivate = 48;
            public static int WBWindowDeactivate = 49;
            public static int WBWindowResize = 50;

            public static int WSActivate = 51;
            public static int WSBeforeDoubleClick = 52;
            public static int WBBeforeRightClick = 53;
            public static int WSCalculate = 54;
            public static int WSChange = 55;
            public static int WSDeactivate = 56;
            public static int WSFollowHyperlink = 57;
            public static int WSSelectionChange = 58;

            public static int TestRoutine = 99;

            #endregion

        }

        #endregion

        #region Constructor

        public Work() {
            Core.AddMenuEntries();
            TecUserObj u = TecUserDataManager.LoadTecUser(Environment.UserName);
            Contracts.SetMyUser(u);
        }

        #endregion

        #region Overrides

        public override void SetTpUser() {
            throw new NotImplementedException();
        }

        #endregion

        #region Properties
        #endregion

        #region Methods

        public void ExcelCore(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, int CoreFunctionNumber) {
            ExcelCore(exApp, wb, sh, CoreFunctionNumber, null);
        }

        public void ExcelCore(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, int CoreFunctionNumber, Range r1) {

            if (wb.Name.Left(2) != sh.Name.Left(2)) {
                return;
            }

            #region ExcelSettings

            SetApplication(exApp, false);

            #endregion

            try {

                //LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "ExcelCore(" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")", false);
                
                #region Contextmenüeinträge

                if (CoreFunctionNumber == FC.ChangeView) {
                    Core.ChangeView(exApp, wb, sh);
                }


                if (CoreFunctionNumber == FC.ChangeViewD2) {
                    Core.ChangeViewD2(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewLeistung) {
                    Core.ChangeViewLeistung(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewOrganisation) {
                    Core.ChangeViewOrganisation(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewTorprofi) {
                    Core.ChangeViewTorprofi(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewUser) {
                    Core.ChangeViewUser(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewVr) {
                    Core.ChangeViewVr(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ChangeViewWeek) {
                    Core.ChangeViewWeek(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ExportInterneLeistungen) {
                    string wbName = wb.Name;
                    Core.ExportJobevidenzIntern(exApp, wb, sh);
                    exApp.Windows[wbName].Activate();
                    wb = exApp.ActiveWorkbook;
                    Core.ExportLeistungenIntern(exApp, wb, sh);
                }

                if (CoreFunctionNumber == FC.ExportLeistungenIntern) {
                    Core.ExportLeistungenIntern(exApp, wb, sh);
                }

                #endregion

                #region Workbook Events

                #region WB Activate

                if (CoreFunctionNumber == FC.WBActivate) {
                    Core.WBActivate(exApp, wb, sh);
                }

                #endregion

                #region WB FollowHyperlink

                if (CoreFunctionNumber == FC.WBSheetFollowHyperlink) {
                    Core.WBSheetFollowHyperlink(exApp, wb, sh, r1);
                }

                #endregion

                #endregion


            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ExcelCode: " + ex.Message, true);
            } finally {

                SetApplication(exApp, true);
            }
        }

        #endregion

        #region Functions

        public bool fnExcelCoreBool(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, int CoreFunctionNumber, bool cancel) {
            #region ExcelSettings

            SetApplication(exApp, false);
            Logger log = new Logger();

            #endregion

            try {

                #region Menüeinträge
                #endregion

                #region Workbook Events

                #region WB Activate

                if (CoreFunctionNumber == FC.WBActivate) {
                    cancel = false;
                    Core.WBActivate(exApp, wb, sh);
                }

                #endregion

                #region WB AfterSave

                if (CoreFunctionNumber == FC.WBAfterSave) {
                    //Core.WBAfterSave(exApp, wb, sh);
                }

                #endregion

                #region WB BeforeClose

                if (CoreFunctionNumber == FC.WBBeforeClose) {
                    //cancel = Core.WbBeforeClose(exApp, wb, sh, cancel);
                }

                #endregion

                #region WB BeforeSave

                if (CoreFunctionNumber == FC.WBBeforeSave) {
                    //Core.WBBeforeSave(exApp, wb, sh, cancel);
                }

                #endregion

                #region WB Deactivate

                if (CoreFunctionNumber == FC.WBDeactivate) {
                    Core.WBDeactivate(exApp, wb, sh);
                }

                #endregion

                #region WB SheetActivate

                if (CoreFunctionNumber == FC.WBSheetActivate) {
                    //Core.WbSheetActivate(exApp, wb, sh);
                }

                #endregion

                #region WB SheetCalculate

                if (CoreFunctionNumber == FC.WBSheetCalculate) {
                    //Core.WbSheetCalculate(exApp, wb, sh);
                }

                #endregion

                #region WB SheetChange

                if (CoreFunctionNumber == FC.WBSheetChange) {
                    //Core.WbSheetChange(exApp, wb, sh);
                }

                #endregion

                #region WB SheetDeactivate

                if (CoreFunctionNumber == FC.WBSheetDeactivate) {
                    //Core.WbSheetDeactivate(exApp, wb, sh);
                }

                #endregion

                #endregion

                #region Worksheet Events

                if (CoreFunctionNumber == FC.WSChange) {
                    //Core.WSChange(exApp, wb, sh, log);
                }

                #endregion

            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "fnExcelCoreBool: " + ex.Message, true);
            } finally {
                SetApplication(exApp, true);
            }
            return cancel;
        }

        public bool fnExcelCoreTarget(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Range target, int CoreFunctionNumber, bool cancel) {
            Logger log = new Logger();
            try {

                //cancel = Core.WSChange(exApp, wb, sh, target, cancel, log);

            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "fnExcelCoreTarget: " + ex.Message, true);
            } 
            return cancel;
        }

        #endregion

        #region Destructor

        ~Work() {
            //LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "Work(Destruktor): End Work", false);
        }

        #endregion

    }
}
